import { Guid } from "@/utils/common";
import OSS from "ali-oss";

let client = new OSS({
  region: "oss-cn-shanghai",
  secure: true,
  accessKeyId: "LTAI5tBMJRRLHVavMLGxuyB3",
  accessKeySecret: "2eqnX2xhJHPXepdyoHqBx2znXIEiHG",
  bucket: "mcsz"
});

// 阿里云OSS文件上传
export const AliyunUpload = async file => {
  let date = new Date();
  let fileType =
    file.type === "image/jpeg"
      ? "jpg"
      : file.type === "application/pdf"
        ? "pdf"
        : "jpg";
  let path = `/tsxzy/${date.getFullYear()}/${date.getMonth() +
    1}/${Guid()}.${fileType}`;
  try {
    let result = await client.put(path, file);
    return result.name;
  } catch (e) {
    console.log(e);
  }
};

// 阿里云端删除文件
export const AliyunDeleteFile = async path => {
  try {
    let result = await client.delete(path);
    return result;
  } catch (e) {
    console.log(e);
  }
};
